import dayjs from 'dayjs';
import { downLoad, request } from '../request';

// 查询字典类型列表
export function getDictListType(params: any) {
  return request({
    // url: '/system/dict/type/list/nest',
    url: '/system/dict/type/list',
    method: 'get',
    params
  });
}

// 查询字典类型详细
export function getType(dictId: any) {
  return request({
    url: `/system/dict/type/${dictId}`,
    method: 'get'
  });
}

// 新增字典类型
export function addType(data: any) {
  return request({
    url: '/system/dict/type',
    method: 'post',
    data
  });
}

// 修改字典类型
export function updateType(data: any) {
  return request({
    url: '/system/dict/type',
    method: 'put',
    data
  });
}

// 删除字典类型
export function delType(dictId: any) {
  return request({
    url: `/system/dict/type/${dictId}`,
    method: 'delete'
  });
}

// 刷新字典缓存
export function refreshCache() {
  return request({
    url: '/system/dict/type/refreshCache',
    method: 'delete'
  });
}

// 获取字典选择框列表
export function optionselect() {
  return request({
    url: '/system/dict/type/optionselect',
    method: 'get'
  });
}

// 导出字典
export function exportDict(data: any) {
  return downLoad({
    url: '/system/dict/type/export',
    method: 'POST',
    data,
    fileName: `字典导出${dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`
  });
}

// 查询字典数据列表
export function getDictItemlistData(query: any) {
  return request({
    url: '/system/dict/data/list',
    method: 'get',
    params: query
  });
}

// 查询字典数据详细
export function getData(dictCode: any) {
  return request({
    url: `/system/dict/data/${dictCode}`,
    method: 'get'
  });
}

// 根据字典类型查询字典数据信息
export function getDicts(dictType: any) {
  return request({
    url: `/system/dict/data/type/${dictType}`,
    method: 'get'
  });
}

// 新增字典数据
export function addData(data: any) {
  return request({
    url: '/system/dict/data',
    method: 'post',
    data
  });
}

// 修改字典数据
export function updateData(data: any) {
  return request({
    url: '/system/dict/data',
    method: 'put',
    data
  });
}

// 删除字典数据
export function delData(dictCode: any) {
  return request({
    url: `/system/dict/data/${dictCode}`,
    method: 'delete'
  });
}

/** 新增字典数据项 */
export function addDictItem(data: any) {
  return request({
    url: '/system/dict/data',
    method: 'post',
    data
  });
}

// 修改字典数据
export function updateDictItem(data: any) {
  return request({
    url: '/system/dict/data',
    method: 'put',
    data
  });
}

/** 删除字典数据线 */
export function deleteDictItem(dictCode: any) {
  return request({
    url: `/system/dict/data/${dictCode}`,
    method: 'delete'
  });
}

/** 导出字典数据 */
export function exportDictData(params: any) {
  return downLoad({
    url: '/system/dict/data/export',
    method: 'GET',
    params,
    fileName: `字典数据导出${dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`
  });
}

/** 刷新字典缓存 */
export function refreshDictCache() {
  return request({
    url: '/system/dict/type/refreshCache',
    method: 'delete'
  });
}
// 查询通知公告
export function systemnoticelist(params: any) {
  return request({
    url: '/system/notice/list',
    method: 'get',
    params
  });
}
export function addNotice(data: any) {
  return request({
    url: '/system/notice',
    method: 'post',
    data
  });
}
export function upNotice(data: any) {
  return request({
    url: '/system/notice',
    method: 'put',
    data
  });
}
export function noticeDelete(data: any) {
  return request({
    url: `/system/notice/${data}`,
    method: 'DELETE'
  });
}
/** 获取字典项用于下拉 */
export function getDictByCode(dictType: any) {
  return request({
    url: '/system/dict/data/list/normal-status',
    method: 'get',
    params: { dictType }
  });
}

/**
 * 同步获取字典数据
 *
 * @param {string} code 字典编码
 * @returns {array} 字典数据
 */
export async function getDictSync(code: string) {
  try {
    const { data } = await getDictByCode(code);
    const dict = data?.map((item: any) => {
      return {
        label: item.dictLabel,
        value: item.dictValue,
        ...item
      };
    });
    return dict;
  } catch (error) {
    return [];
  }
}

/**
 * 异步获取字典数据
 *
 * @param {string} code 字典编码
 * @returns {promises}
 */
export function getDict(code: string) {
  return new Promise((resolve, reject) => {
    getDictByCode(code).then(({ data, error }) => {
      if (!error) {
        const dict = data?.map((item: any) => {
          return {
            label: item.dictLabel,
            value: item.dictValue,
            ...item
          };
        });
        resolve(dict);
      } else {
        reject(error);
      }
    });
  });
}
